<template>
<!-- 添加用户 -->
    <el-form :model="addForm" :rules="rules" ref="addForm" label-width="100px" class="demo-ruleForm">
         <el-form-item label="头像" prop="name">
            <el-upload
            class="avatar-uploader"
            action="http://localhost:8080/api/file/fileUpload"
            :headers="headers"
            name="file"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
            <el-input v-model="addForm.username" style="width:300px;"></el-input>
        </el-form-item>
         <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="addForm.password" style="width:300px;"></el-input>
        </el-form-item>
         <el-form-item label="性别">
            <el-radio-group v-model="addForm.sex">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="0">女</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="身份证号码" prop="idCard">
            <el-input v-model="addForm.idCard" style="width:300px;"></el-input>
        </el-form-item>
        <el-form-item label="电话号码" prop="phone">
            <el-input v-model="addForm.phone" style="width:300px;"></el-input>
        </el-form-item>
        <el-form-item label="家庭住址">
            <el-input v-model="addForm.address" style="width:300px;"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
            <el-input v-model="addForm.email" style="width:300px;"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('addForm')">立即创建</el-button>
            <el-button @click="resetForm('addForm')">重置</el-button>
        </el-form-item>
    </el-form>
</template>
<script>
import Qs from 'qs';
  export default {
    name:"UserAdd",
    data() {
      return {
        imageUrl:"",
        headers:{
          token: window.sessionStorage.getItem('token')
        },
        addForm:{
          avatar: 0,
          username:"",
          password:"",
          sex:0,
          idCard:"",
          phone:"",
          address:"",
          email:""
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
          ],
          password: [
            { required: true, message: '请输入用户密码', trigger: 'change' },
            { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
          ],
          idCard: [
            { required: true, message: '请填写身份证号码', trigger: 'blur'}
          ],
          phone: [
            { required: true, message: '请填写电话号码', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '请填写邮箱地址', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
        // 提交表单
      submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            const res = await this.$http.post("/user/addUser",Qs.stringify(this.addForm))
            if(res.data.code === 200){
              this.$message({
                message: res.data.msg,
                type: 'success'
              });
              // 刷新当前页面
              this.$refs[formName].resetFields();
            }else{
                  this.$message.error(res.data.msg);
            }
          } else {
            return false;
          }
        });
      },
    //   重置表单
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
       // 处理头像提交成功事件
    handleAvatarSuccess(res, file) {
      if(res.code === 200){
         this.$message({
          message: res.msg,
          type: 'success'
        });
        this.addForm.avatar = res.data;
        this.imageUrl = URL.createObjectURL(file.raw);
      }else{
        this.$message({
          showClose: true,
          message: res.msg,
          type: 'error'
        });
      }
       
    },
    //文件上传前处理
    beforeAvatarUpload(file) {
        // const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;
        // if (!isJPG) {
        //     this.$message.error('上传头像图片只能是 JPG 格式!');
        // }
        if (!isLt2M) {
            this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        // return isJPG && isLt2M;
        return isLt2M;
    }

    },
   
  }
</script>

<style scoped>
/* 文件上传 */
.el-upload {
    border: 1px dashed #750e0e;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
.el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 100%;
    height: 100%;
    display: block;
  }
  .avatar-uploader{
    width: 178px;
    height: 178px;
    border: 1px solid #b2b8c0;
    margin-left: 50px;
  }
</style>